package com.last1mile.web.control;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.last1mile.model.Login;
import com.last1mile.pojo.MerServeDO;
import com.last1mile.service.ServeService;
import com.last1mile.service.UserService;

@Controller
@RequestMapping("/index*.html")
@SessionAttributes("currentLogin")
public class IndexController {

    @Autowired
    @Qualifier("userServiceImpl")
    UserService  userService;

    @Autowired
    @Qualifier("serveServiceImpl")
    ServeService serveService;

    @RequestMapping(value = "/index.html", method = RequestMethod.GET)
    public String index(HttpServletRequest request, ModelMap model) {
        Integer merId = null;
        String addrCountry = "001"; // TODO 当前设置成缺省值：中国代码001 以后要改成用户访问的来源国家
        String addrState = null;
        String addrCity = null;
        String addrDistrict = null;

        if (request.getSession().getAttribute("currentLogin") != null) {
            Login login = (Login) request.getSession().getAttribute("currentLogin");
            model.addAttribute("currentLogin", login);
            merId = login.getUserId();
            addrCountry = login.getCoutryCode();
            // TODO 根据用户的取得它所属的addr
        }
        

        List<MerServeDO> serveList = this.serveService.queryServeByAddrExceptUser(addrCountry, addrState, addrCity,
                                                                                  addrDistrict, merId);
        model.addAttribute("serveList", serveList);

        return "index";
    }
}
